package com.ruoyi.web.controller.analysiswarning;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.analysiswarning.domain.AnalyOverview;
import com.ruoyi.analysiswarning.domain.ScriptApp;
import com.ruoyi.analysiswarning.domain.vo.OverViewVo;
import com.ruoyi.analysiswarning.mapper.AnalysiswarningMapper;
import com.ruoyi.analysiswarning.mapper.ScriptAppMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/createData")
public class CreateDateController {

	private static Map<String,Integer> typeUtil;

	static {
		typeUtil = new HashMap<>();
		typeUtil.put("1",2);
		typeUtil.put("2",3);
		typeUtil.put("3",4);
		typeUtil.put("11",5);
		typeUtil.put("12",6);
	}

	@Autowired
	ScriptAppMapper scriptAppMapper;
	@Autowired
	AnalysiswarningMapper analysiswarningMapper;

	@Scheduled(cron = "0 0 4 * * ?")
	public void createDataOne() {

		List<ScriptApp> scriptApps = scriptAppMapper.selectList(new QueryWrapper<>());

		// 打乱排序
		Collections.shuffle(scriptApps);

		// 生成随机条数
		for (int i = 0; i < scriptApps.size(); i++) {
			handleScore(scriptApps.get(i), i);
		}

		// 取出所有类型
		List<String> types = scriptApps.stream().map(x -> x.getType()).distinct().collect(Collectors.toList());

		// 重置告警表
		DateTime yesterday = DateUtil.yesterday();
		String format = DateUtil.format(yesterday, "yyyy-MM-dd");
		analysiswarningMapper.resetAlarm(format);

		// 算出显示类型
		int dailyDisplayType = DateUtil.dayOfMonth(new Date()) % 3;

		for (String type : types) {

			if (!typeUtil.containsKey(type)){
				continue;
			}

			List<ScriptApp> oneType = scriptApps.stream().filter(x -> x.getType().equals(type) && String.valueOf(dailyDisplayType).equals(x.getDailyDisplay())).collect(Collectors.toList());

			String count = oneType.stream().map(x -> x.getScore()).reduce((x, y) -> String.valueOf(Integer.parseInt(x) + Integer.parseInt(y))).get();

			int handleNum = Integer.parseInt(count);

			List<AnalyOverview> analyOverviews = analysiswarningMapper.queryList(new OverViewVo().setTbId(typeUtil.get(type)));

			// 需要隐藏的条数
			handleNum = analyOverviews.size() - handleNum;

			for (int i = 0; i < handleNum; i++) {

				AnalyOverview analyOverview = analyOverviews.get(i);
				// 设置改属性的删除状态为已删除
				analysiswarningMapper.fakerDeleteAnalyOverviews(analyOverview);

			}

		}


	}

	public void handleScore(ScriptApp scriptApp, int i) {
		Integer total;

		while (true) {
			Double totalDouble = Math.random() * 1500;
			total = totalDouble.intValue();
			if (total > 203 && total < 1650) {
				break;
			}

		}

		while (true) {
			Double scoreRandom = Math.random() * 5;
			Integer score = scoreRandom.intValue();
			if (score > 1 && score < 5) {
				scriptApp.setScore(String.valueOf(score));
				scriptApp.setTotal(String.valueOf(total));
				scriptApp.setScriptPath(new StringBuffer().append("/usr/local/scripts/shell/task-").append(i).append(".sh").toString());
				break;
			}
		}

		scriptAppMapper.updateById(scriptApp);
	}


}
